# Supabase

Langchain supports using Supabase Postgres database as a vector store, using the `pgvector` postgres extension. Refer to the [Supabase blog post](https://supabase.com/blog/openai-embeddings-postgres-vector) for more information.

## Setup

### Install the library with

```bash npm2yarn
npm install -S @supabase/supabase-js
```

### Create a table and search function in your database

Run this in your database:

```sql
-- Enable the pgvector extension to work with embedding vectors
create extension vector;

-- Create a table to store your documents
create table documents (
  id bigserial primary key,
  content text, -- corresponds to Document.pageContent
  metadata jsonb, -- corresponds to Document.metadata
  embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);

-- Create a function to search for documents
create function match_documents (
  query_embedding vector(1536),
  match_count int DEFAULT null,
  filter jsonb DEFAULT '{}'
) returns table (
  id bigint,
  content text,
  metadata jsonb,
  embedding jsonb,
  similarity float
)
language plpgsql
as $$
#variable_conflict use_column
begin
  return query
  select
    id,
    content,
    metadata,
    (embedding::text)::jsonb as embedding,
    1 - (documents.embedding <=> query_embedding) as similarity
  from documents
  where metadata @> filter
  order by documents.embedding <=> query_embedding
  limit match_count;
end;
$$;
```

## Usage

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai @langchain/community
```

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/indexes/vector_stores/supabase.ts";
import MetadataFilterExample from "@examples/indexes/vector_stores/supabase_with_metadata_filter.ts";
import MetadataQueryBuilderFilterExample from "@examples/indexes/vector_stores/supabase_with_query_builder_metadata_filter.ts";
import MaximumMarginalRelevanceExample from "@examples/indexes/vector_stores/supabase_with_maximum_marginal_relevance.ts";
import DeletionExample from "@examples/indexes/vector_stores/supabase_deletion.ts";

### Standard Usage

The below example shows how to perform a basic similarity search with Supabase:

<CodeBlock language="typescript">{Example}</CodeBlock>

### Metadata Filtering

Given the above `match_documents` Postgres function, you can also pass a filter parameter to only documents with a specific metadata field value. This filter parameter is a JSON object, and the `match_documents` function will use the Postgres JSONB Containment operator `@>` to filter documents by the metadata field values you specify. See details on the [Postgres JSONB Containment operator](https://www.postgresql.org/docs/current/datatype-json.html#JSON-CONTAINMENT) for more information.

**Note:** If you've previously been using `SupabaseVectorStore`, you may need to drop and recreate the `match_documents` function per the updated SQL above to use this functionality.

<CodeBlock language="typescript">{MetadataFilterExample}</CodeBlock>

### Metadata Query Builder Filtering

You can also use query builder-style filtering similar to how [the Supabase JavaScript library works](https://supabase.com/docs/reference/javascript/using-filters) instead of passing an object. Note that since most of the filter properties are in the metadata column, you need to use arrow operators (`->` for integer or `->>` for text) as defined in [Postgrest API documentation](https://postgrest.org/en/stable/references/api/tables_views.html?highlight=operators#json-columns) and specify the data type of the property (e.g. the column should look something like `metadata->some_int_value::int`).

<CodeBlock language="typescript">{MetadataQueryBuilderFilterExample}</CodeBlock>

### Maximal marginal relevance

You can use maximal marginal relevance search, which optimizes for similarity to the query AND diversity.

**Note:** If you've previously been using `SupabaseVectorStore`, you may need to drop and recreate the `match_documents` function per the updated SQL above to use this functionality.

<CodeBlock language="typescript">{MaximumMarginalRelevanceExample}</CodeBlock>

### Document deletion

<CodeBlock language="typescript">{DeletionExample}</CodeBlock>
